package cn.itcalvin.sncoal.cth.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.itcalvin.sncoal.cth.domain.InfoNews;
import cn.itcalvin.sncoal.cth.pojo.common.ReqLong;
import cn.itcalvin.sncoal.cth.pojo.common.RespUserInfo;
import cn.itcalvin.sncoal.cth.pojo.req.news.ReqNewsAdd;
import cn.itcalvin.sncoal.cth.pojo.req.news.ReqNewsList;
import cn.itcalvin.sncoal.cth.pojo.req.news.ReqNewsModify;
import cn.itcalvin.sncoal.cth.pojo.response.news.RespNewsList;
import cn.itcalvin.sncoal.cth.utils.JwtUtil;
import cn.itcalvin.sncoal.cth.utils.SnowFlakeUtils;
import cn.itcalvin.vo.Message;
import cn.itcalvin.vo.MessageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;

import cn.itcalvin.sncoal.cth.mapper.main.InfoNewsMapper;
import cn.itcalvin.sncoal.cth.service.InfoNewsService;

import java.util.List;

@Service
public class InfoNewsServiceImpl implements InfoNewsService{

    @Autowired
    private InfoNewsMapper infoNewsMapper;
    /**
     * 新增
     *
     * @param reqNewsAdd
     * @return
     */
    @Override
    public Message add(ReqNewsAdd reqNewsAdd) {
        final RespUserInfo userInfo = JwtUtil.getUserInfo();
        InfoNews infoNews=new InfoNews(){{
            setCreateId(userInfo.getId());
            setCreateTime(DateUtil.date());
            setId(SnowFlakeUtils.getId());
            setIsDelete(false);
            setTitle(reqNewsAdd.getTitle());
            setState(reqNewsAdd.getState());
            setRemark(reqNewsAdd.getRemark());
            setPulishTime(reqNewsAdd.getPulishTime());
            setNewsType(reqNewsAdd.getNewsType());
        }};
        infoNewsMapper.insert(infoNews);
        return MessageUtil.success();
    }

    /**
     * 修改
     *
     * @param reqNewsModify
     * @return
     */
    @Override
    public Message modify(ReqNewsModify reqNewsModify) {
        final RespUserInfo userInfo = JwtUtil.getUserInfo();
        final InfoNews infoNews = infoNewsMapper.selectByPrimaryKey(reqNewsModify.getId());
        if(infoNews==null){
            return MessageUtil.error("数据获取失败！");
        }
        infoNews.setTitle(reqNewsModify.getTitle());
        infoNews.setState(reqNewsModify.getState());
        infoNews.setRemark(reqNewsModify.getRemark());
        infoNews.setPulishTime(reqNewsModify.getPulishTime());
        infoNews.setNewsType(reqNewsModify.getNewsType());
        infoNews.setModifyId(userInfo.getId());
        infoNews.setModifyTime(DateUtil.date());
        infoNewsMapper.updateByPrimaryKey(infoNews);
        return MessageUtil.success();
    }

    /**
     * 删除
     *
     * @param reqLong
     * @return
     */
    @Override
    public Message delete(ReqLong reqLong) {
        final RespUserInfo userInfo = JwtUtil.getUserInfo();
        final InfoNews infoNews = infoNewsMapper.selectByPrimaryKey(reqLong.getId());
        if(infoNews==null){
            return MessageUtil.error("数据获取失败！");
        }
        infoNews.setDeleteId(userInfo.getId());
        infoNews.setDeleteTime(DateUtil.date());
        infoNews.setIsDelete(true);
        infoNewsMapper.updateByPrimaryKey(infoNews);
        return MessageUtil.success();
    }

    /**
     * 查询
     *
     * @param reqNewsList
     * @return
     */
    @Override
    public Message<List<RespNewsList>> list(ReqNewsList reqNewsList) {
        PageHelper.startPage(reqNewsList.getPageIndex(),reqNewsList.getPageSize());
        List<RespNewsList> list=infoNewsMapper.list(reqNewsList);
        PageInfo<RespNewsList> pageInfo=new PageInfo<>(list);
        return MessageUtil.success(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 详情
     *
     * @param reqLong
     * @return
     */
    @Override
    public Message<RespNewsList> details(ReqLong reqLong) {
        final InfoNews infoNews = infoNewsMapper.selectByPrimaryKey(reqLong.getId());
        if(infoNews==null){
            return MessageUtil.error("数据获取失败！");
        }
        return MessageUtil.success(new RespNewsList(){{
            setCreateTime(DateUtil.date());
            setId(SnowFlakeUtils.getId());
            setTitle(infoNews.getTitle());
            setState(infoNews.getState());
            setRemark(infoNews.getRemark());
            setPulishTime(infoNews.getPulishTime());
            setNewsType(infoNews.getNewsType());
        }});
    }
}
